//
//  SmsCloudNetManger.m

#import "SmsCloudNetManger.h"
@interface SmsCloudNetManger()
@property (strong, nonatomic) AFHTTPSessionManager *smsCloudMag;
@end
@implementation SmsCloudNetManger
static SmsCloudNetManger *defaultMag = nil;
 + (SmsCloudNetManger*)defaultMag{
     static dispatch_once_t token;
     dispatch_once(&token, ^{
         if(defaultMag == nil){
             defaultMag = [[SmsCloudNetManger alloc] init];
             defaultMag.smsCloudMag = [AFHTTPSessionManager manager];
             defaultMag.smsCloudMag.requestSerializer = [AFHTTPRequestSerializer serializer];
             defaultMag.smsCloudMag.responseSerializer = [AFHTTPResponseSerializer serializer];
             defaultMag.smsCloudMag.requestSerializer.timeoutInterval = 15.0;
         }
     });
     return defaultMag;
}
- (void)smsCloudNetGet:(NSString *)url withParam:(NSDictionary *)paramDic withSuccess:(void(^)(id data))successBlack withFail:(void(^)(void))failBlack{
    [self.smsCloudMag GET:url parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if([jsonDict[@"code"] intValue] == 200){
                successBlack(jsonDict);
            }else{
                [SVProgressHUD showWithStatus:jsonDict[@"msg"]];
                failBlack();
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlack();
    }];
}
- (void)smsCloudNetPost:(NSString *)url withParam:(NSDictionary *)paramDic withSuccess:(void(^)(id data))successBlack withFail:(void(^)(void))failBlack{
    [self.smsCloudMag POST:url parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"请求接口：%@",url);
            NSLog(@"请求参数：%@",paramDic);
            NSLog(@"返回结果：%@",jsonDict);
            if([jsonDict[@"code"] intValue] == 200){
                successBlack(jsonDict);
            }else{
                [SVProgressHUD showInfoWithStatus:jsonDict[@"msg"]];
                failBlack();
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlack();
    }];
}
@end
